#include "../inc/Ui/WeatherForm.h"
#include "../inc/Utility/MyParserXml.h"
#include "../inc/Utility/MyParserTiengViet.h"

using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Media;
using namespace Osp::Graphics;

int WeatherForm::FormId = -1;

WeatherForm::WeatherForm(void) {
}

WeatherForm::~WeatherForm(void) {
}

bool WeatherForm::Initialize() {
	Form::Construct(L"WeatherForm");

	return true;
}

result WeatherForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	pThongBao = static_cast<Label*> (GetControl("IDC_LABEL2"));
	pEditFieldDiaChi = static_cast<EditField*> (GetControl("IDC_ADDRESS"));
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_XEMTHOITIET);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);

		return r;
}

result WeatherForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void WeatherForm::OnActionPerformed(const Control& source, int actionId) {
	// TODO: Add your implementation codes here
	String addressWeb;
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	switch (actionId) {
	case ID_SOFTKEY_XEMTHOITIET: {
		if (pEditFieldDiaChi->GetTextLength() == 0) {
			MessageBox msg;
			msg.Construct("Chào bạn", "Bạn chưa nhập địa chỉ cần tìm",
					MSGBOX_STYLE_OK, 3000);
			int i = 0;
			msg.ShowAndWait(i);
			return;
		} else {
			String address;
			MyUtil::StringToUTF8String(pEditFieldDiaChi->GetText(),address);
			addressWeb.Format(500, L"www.google.com/ig/api?weather=%S",address.GetPointer());
			Downloader::downloadAndSave(addressWeb,L"/Home/ThongTinThoiTiet.xml", this);
				}
		}
		break;
		case ID_SOFTKEY_BACK:
		AppLog("Back");
		pFormManager->SetCurrentForm(WebForm::GetFormId());

		break;
		default:
		break;
	}

}
void WeatherForm::OnItemStateChanged(const Osp::Ui::Control &source, int index,
		int itemId, Osp::Ui::ItemStatus status) {

}

void WeatherForm::SetParentForm(Form *pParent) {
	//this->pFormParent = pParent;
}

void WeatherForm::SetFrame(Frame *pFrame) {
	//this->pFrame = pFrame;
}

void WeatherForm::HienThiThongTinThoiTiet() {
	MyCurrentWeather*curWeather;
	MyParserXml *myParserXml = new MyParserXml();
	curWeather = myParserXml->ThoiTietTrongNgay("/Home/ThongTinThoiTiet.xml");
	if (myParserXml->checkHaveInfoWeather() == false) {
		pThongBao->SetText("Địa chỉ nhập không hợp lệ");
	} else {
		pThongBao->SetText("");
		//pFrame->SetCurrentForm(*pWeatherInfoForm);
		//pWeatherInfoForm->RequestRedraw(true);
		FormManager *pFormManager = null;
		FormManager::GetInstance(pFormManager);
		pFormManager->SetCurrentForm(WeatherInfoForm::GetFormId());
		Form *pWeatherInfoForm = null;
		 result r = pFormManager->GetForm(WeatherInfoForm::GetFormId(), pWeatherInfoForm);
		 if (IsFailed(r)){
		 return;
		 }
		static_cast<WeatherInfoForm*>(pWeatherInfoForm)->DisplayCurrentWeatherAndForecast();
		}
	Draw();
	Show();
}

void WeatherForm::OnDownloadComplete(String url, String path) {
	AppLog("Download successful! \n");
	HienThiThongTinThoiTiet();
}

//called  if there's a network failure, http error or on failed save
void WeatherForm::OnDownloadFailed(String url, String path) {
	AppLog("Download failed! \n");

}
